<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\SettingUserTags;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class UserTagController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('用户标签管理');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('标签编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('标签创建');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new SettingUserTags());
        $grid->model()->orderBy('id', 'desc');
        $grid->id('标签ID')->sortable();
        $grid->tag_name('标签名')->editable();
        $grid->tag_type('标签分类')->display(function ($data) {
            return SettingUserTags::TYPE[$data] ?? "未知分类";
        });
        $grid->tag_gender('标签所属性别')->display(function ($data) {
            return $data == -1 ? '不分' : ($data == 1 ? '男' : '女');
        });
        $grid->flag('状态')->display(function ($data) {
            return $data == 1 ? '有效' : '无效';
        });
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new SettingUserTags());
        $form->text('tag_name', '标签名')->required();
        $form->select('tag_type', '类别')->options(SettingUserTags::TYPE)->default(1)->required();
        $form->select('tag_gender', '性别')->options([-1 => '不分', 1 => '男', 0 => '女'])->default(-1)->required();
        $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
        return $form;
    }
}
